define("UniWorker", [], () => {
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/ 
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ 	// define getter functions for harmony exports
/******/ 	__webpack_require__.d = (exports, definition) => {
/******/ 		for(var key in definition) {
/******/ 			if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 				Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 			}
/******/ 		}
/******/ 	};
/******/ })();
/******/ 
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ 	__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/ 
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = (exports) => {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/ })();
/******/ 
/************************************************************************/
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ });
var plusMessageCallBackList = new Map();
plus.globalEvent.addEventListener("plusMessage", function (e) {
    if (e.data.type === "UniWorker" && plusMessageCallBackList.has(e.originId)) {
        plusMessageCallBackList.get(e.originId)(parse(JSON.parse(e.data.args || {})));
    }
});
var Thread = /** @class */ (function () {
    function Thread(id, worker) {
        this.id = id;
        this.worker = worker;
    }
    Thread.prototype.onMessage = function (callback) {
        this.messageCallBack = callback;
        return this;
    };
    Thread.prototype.postMessage = function (message) {
        var _a;
        if (this.worker) {
            (_a = this.worker) === null || _a === void 0 ? void 0 : _a.evalJS("postWorkerMessage(".concat(this.id, ", ").concat(stringify(message), ")"));
        }
        return this;
    };
    Thread.prototype.onProcessKilled = function (callback) {
        this.killCallBack = callback;
        return this;
    };
    Thread.prototype.terminate = function () {
        if (this.worker) {
            this.messageCallBack = undefined;
            this.worker.evalJS("terminate(".concat(this.id, ")"));
            // this.worker.removeFromParent();
            // this.worker.close();
            this.worker.terminate(this.id);
        }
    };
    return Thread;
}());
var UniWorker = /** @class */ (function () {
    function UniWorker() {
        this.threadQueue = new Map();
    }
    UniWorker.getInstance = function () {
        if (!UniWorker.instance) {
            UniWorker.instance = new UniWorker();
        }
        return UniWorker.instance;
    };
    UniWorker.prototype.create = function (url, max) {
        var _this = this;
        if (max === void 0) { max = 1; }
        return new Promise(function (resolve, reject) {
            var _a;
            var id = new Date().getTime();
            if (_this.worker) {
                var thread = new Thread(id, _this.worker);
                _this.threadQueue.set(id, thread);
                (_a = _this.worker) === null || _a === void 0 ? void 0 : _a.evalJS("create(".concat(id, ", \"").concat(url, "\", ").concat(max, ")"));
                resolve(thread);
            }
            else {
                _this.worker = plus.webview.create("/uni_modules/xzh-worker/hybrid/html/index.html", "UniWorker".concat(new Date().getTime()), {
                    opacity: 0,
                    width: "0px",
                    height: "0px",
                    "uni-app": "none",
                    blockNetworkImage: true,
                    webviewBGTransparent: true,
                });
                _this.worker.setBlockNetworkImage(true);
                _this.worker.setContentVisible(false);
                _this.worker.setVisible(false);
                _this.worker.addEventListener("close", function (res) {
                    var _a;
                    plusMessageCallBackList.delete((_a = _this.worker) === null || _a === void 0 ? void 0 : _a.__uuid__);
                    _this.threadQueue.forEach(function (thread) {
                        thread.killCallBack && thread.killCallBack();
                        thread.terminate();
                    });
                    _this.worker = undefined;
                });
                _this.worker.addEventListener("error", function (error) { return reject(error); });
                _this.worker.addEventListener("loaded", function () {
                    var _a;
                    var thread = new Thread(id, _this.worker);
                    _this.threadQueue.set(id, thread);
                    (_a = _this.worker) === null || _a === void 0 ? void 0 : _a.evalJS("create(".concat(id, ", \"").concat(url, "\", ").concat(max, ")"));
                    resolve(thread);
                });
                _this.worker.terminate = function (id) {
                    var _a;
                    (_a = _this.threadQueue) === null || _a === void 0 ? void 0 : _a.delete(id);
                };
                // this.worker.addEventListener("titleUpdate", (e: any) => {
                //   if (e.title === "uni-worker") return;
                //   console.log(e.title.length);
                //   this.messageCallBack && this.messageCallBack(parse(JSON.parse(e.title)));
                // });
                plusMessageCallBackList.set(_this.worker.__uuid__, function (data) {
                    _this.threadQueue.forEach(function (thread) {
                        thread.messageCallBack && thread.messageCallBack(data);
                    });
                });
                plus.webview.all()[0].append(_this.worker);
            }
        });
    };
    return UniWorker;
}());
function stringify(data) {
    // #ifdef VUE2
    if (typeof data === "object") {
        return JSON.stringify(data, function (key, value) {
            if (value instanceof ArrayBuffer) {
                return {
                    type: "ArrayBuffer",
                    // value: Array.prototype.slice.call(new Uint8Array(value))
                    value: "[".concat(Buffer.from(value).toJSON().data.toString(), "]"),
                };
            }
            else if (value instanceof Uint8Array) {
                return {
                    type: "Uint8Array",
                    // value: Array.prototype.slice.call(value),
                    value: "[".concat(Buffer.from(value).toJSON().data.toString(), "]"),
                };
            }
            else if (value instanceof Int8Array) {
                return {
                    type: "Int8Array",
                    // value: Array.prototype.slice.call(value),
                    value: "[".concat(Buffer.from(value).toJSON().data.toString(), "]"),
                };
            }
            else if (value instanceof Int16Array) {
                return {
                    type: "Int16Array",
                    // value: Array.prototype.slice.call(value),
                    value: "[".concat(Buffer.from(value).toJSON().data.toString(), "]"),
                };
            }
            else if (value instanceof Int32Array) {
                return {
                    type: "Int32Array",
                    // value: Array.prototype.slice.call(value),
                    value: "[".concat(Buffer.from(value).toJSON().data.toString(), "]"),
                };
            }
            return value;
        });
    }
    // #endif
    // #ifdef VUE3
    if (typeof data === "object") {
        return JSON.stringify(data, function (key, value) {
            if (value instanceof ArrayBuffer) {
                return {
                    type: "ArrayBuffer",
                    value: Array.prototype.slice.call(new Uint8Array(value)),
                };
            }
            else if (value instanceof Uint8Array) {
                return {
                    type: "Uint8Array",
                    value: Array.prototype.slice.call(value),
                };
            }
            else if (value instanceof Int8Array) {
                return {
                    type: "Int8Array",
                    value: Array.prototype.slice.call(value),
                };
            }
            else if (value instanceof Int16Array) {
                return {
                    type: "Int16Array",
                    value: Array.prototype.slice.call(value),
                };
            }
            else if (value instanceof Int32Array) {
                return {
                    type: "Int32Array",
                    value: Array.prototype.slice.call(value),
                };
            }
            return value;
        });
    }
    // #endif
    return data;
}
function parse(obj, list) {
    var newObj = {};
    if (typeof obj === "object") {
        if (obj.type) {
            switch (obj.type) {
                case "ArrayBuffer":
                    newObj = new Uint8Array(obj.value).buffer;
                    list === null || list === void 0 ? void 0 : list.push(newObj);
                    return newObj;
                case "Uint8Array":
                    newObj = new Uint8Array(obj.value);
                    list === null || list === void 0 ? void 0 : list.push(newObj);
                    return newObj;
                case "Int8Array":
                    newObj = new Int8Array(obj.value);
                    list === null || list === void 0 ? void 0 : list.push(newObj);
                    return newObj;
                case "Int16Array":
                    newObj = new Int16Array(obj.value);
                    list === null || list === void 0 ? void 0 : list.push(newObj);
                    return newObj;
                case "Int32Array":
                    newObj = new Int32Array(obj.value);
                    list === null || list === void 0 ? void 0 : list.push(newObj);
                    return newObj;
                default:
                    Object.keys(obj).forEach(function (item) {
                        newObj[item] = parse(obj[item], list);
                    });
                    break;
            }
        }
        else if (obj instanceof Array) {
            return obj;
        }
        else {
            Object.keys(obj).forEach(function (item) {
                newObj[item] = parse(obj[item], list);
            });
        }
    }
    else {
        return obj;
    }
    return newObj;
}
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (UniWorker);

__webpack_exports__ = __webpack_exports__["default"];
/******/ return __webpack_exports__;

});